// pages/mall/order/index.js
const app = getApp()
const config = require('../../../utils/config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    product: null,
    productNum: 1,
    cod: 1,
    orderDesc: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(app.globalData.product)
    this.setData({
      product: app.globalData.product,
    })
  },
  addOrder() {
    let _this = this
    wx.request({
      url: config.HOST + '/productOrder/newOrder',
      data: {
        token: wx.getStorageSync('token'),
        productId: this.data.product.productId,
        number: this.data.productNum,
        orderDesc: this.data.orderDesc,
        cod: this.data.cod
      },
      success(res) {
        console.log(res)
        if (res.data.status == 200 && res.data.data == 1) {
          wx.redirectTo({
            url:'../orderResult/index?cod='+_this.data.cod
          })
        } else {
          wx.showToast({
            title: '订单创建失败',
            icon: 'none'
          })
        }
      }
    })
  },
  changeNumber(e) {
    let n = parseInt(e.currentTarget.dataset.add)
    if (this.data.productNum + n <= 0) {
      return null;
    }
    if (this.data.productNum + n > this.data.product.stock) {
      wx.showToast({
        title: '最大库存了',
        icon: 'none'
      })
      return null;
    }
    this.setData({
      productNum: this.data.productNum + n
    })
  },
  changeCod(e) {
    this.setData({
      cod: e.currentTarget.dataset.cod
    })
  },
  inputDesc(event) {
    this.setData({
      orderDesc: event.detail.value
    })
  }
})